home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / speech recognition sample / listeningae.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.5 KB  |  117 lines

  1. /*
  2.     File:        ListeningAE.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/2/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #ifndef __LISTENINGAE__
  24. #include "ListeningAE.h"
  25. #endif
  26.  
  27. OSErr        SetSpeechDoneAEHander    (SpeechInfoPtr theSpeechInfo)
  28. {
  29.     OSErr        theErr        = noErr;
  30.  
  31.     if (theSpeechInfo->SpeechDoneUPP != nil) {
  32.         DisposeRoutineDescriptor (theSpeechInfo->SpeechDoneUPP);
  33.     }
  34.     theSpeechInfo->SpeechDoneUPP = NewAEEventHandlerProc (HandleSpeechDoneAppleEvent);
  35.  
  36.     return theErr;
  37. }
  38.  
  39. OSErr        InstallRecogAEHandler    (SpeechInfoPtr theSpeechInfo)
  40. {
  41.     OSErr        theErr        = noErr;
  42.  
  43.     /* Install an AppleEvent handler so recognizer can send us recognition results. */
  44.     theErr = AEInstallEventHandler(kAESpeechSuite, kAESpeechDone, theSpeechInfo->SpeechDoneUPP, (long)theSpeechInfo, false);
  45.  
  46.     return theErr;
  47. }
  48.  
  49. pascal OSErr HandleSpeechDoneAppleEvent (AppleEvent *theAEevt, AppleEvent* reply, SpeechInfoPtr theSpeechInfo)
  50. {
  51. #pragma unused (reply)
  52.  
  53.     SRRecognitionResult    recResult;
  54.     DescType            actualType;
  55.     long                actualSize;
  56.     OSErr                theErr            = noErr,
  57.                         recStatus        = noErr;
  58.  
  59.     /* Get status */
  60.     theErr = AEGetParamPtr (theAEevt, keySRSpeechStatus, typeShortInteger, &actualType, (Ptr)&recStatus, sizeof(recStatus), &actualSize);
  61.  
  62.     /* Get result */
  63.     if (theErr == noErr && recStatus == noErr) {
  64.         theErr = AEGetParamPtr (theAEevt, keySRSpeechResult, typeSRSpeechResult, &actualType, (Ptr)&recResult, sizeof(SRRecognitionResult), &actualSize);
  65.     }
  66.     if (theErr == noErr) {
  67.         theErr = ParseResult (&recResult, theSpeechInfo);
  68.     }
  69.  
  70.     return theErr;
  71. }
  72.  
  73. /*
  74.     Here we figure out what the user said and act on what they said.
  75. */
  76. OSErr        ParseResult            (SRRecognitionResult *recResult, SpeechInfoPtr theSpeechInfo)
  77. {
  78.     SRLanguageModel        theResultLM;
  79.     SRPath                theResultPath;
  80.     SRLanguageObject    theSubElement;
  81.     SRSpeechObject        speechRefCon    = 0;
  82.     long                count            = 0,
  83.                         i;
  84.     Size                theLen;
  85.     OSErr                theErr            = noErr;
  86.  
  87.     theLen = sizeof (theResultLM);
  88.     theErr = SRGetProperty (*recResult, kSRLanguageModelFormat, &theResultLM, &theLen);
  89.     if (theErr == noErr) {
  90.         theLen = sizeof (theResultPath);
  91.         theErr = SRGetProperty (*recResult, kSRPathFormat, &theResultPath, &theLen);
  92.         if (theErr == noErr) {
  93.             theErr = SRCountItems (theResultPath, &count);
  94.         }
  95.         if (theErr == noErr) {
  96.             for (i = 0; i < count; i++) {
  97.                 theLen = sizeof (speechRefCon);
  98.                 theErr = SRGetIndexedItem (theResultPath, &theSubElement, i);
  99.                 if (theErr == noErr) {
  100.                     theErr = SRGetProperty (theSubElement, kSRRefCon, &speechRefCon, &theLen);
  101.                     if (speechRefCon != 0) {
  102.                         /* ParseRefCon is the routine that does the work of doing what the
  103.                            user said to do. */
  104.                         theErr = ParseRefCon (theSpeechInfo, (CommandPtr)speechRefCon);
  105.                         SRReleaseObject (speechRefCon);
  106.                     }
  107.                     SRReleaseObject (theSubElement);
  108.                 }
  109.             }
  110.         }
  111.         SRReleaseObject (theResultPath);
  112.     }
  113.  
  114.     SRReleaseObject (theResultLM);
  115.     return theErr;
  116. }
  117.